/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.AccountDAO;
import Models.Account;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyro
 */
@ManagedBean
@RequestScoped
public class AccountController {

    String hideLogin, showBox;

    public String getHideLogin() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            if (session.getAttribute("account") == null || session.getAttribute("account").equals("")) {
                return "block";
            } else {
                return "none";
            }
        } catch (Exception ex) {
            return "block";
        }

    }

    public void setHideLogin(String hideForm) {
        this.hideLogin = hideForm;
    }

    public String getShowBox() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            if (session.getAttribute("account") == null || session.getAttribute("account").equals("")) {
                return "none";
            } else {
                return "block";
            }
        } catch (Exception ex) {
            return "none";
        }
    }

    public void setShowBox(String showBox) {
        this.showBox = showBox;
    }

    public void checkValidUser() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        Account a1 = (Account) (FacesContext.getCurrentInstance().getExternalContext().
                getRequestMap().get("accountBean"));
        Account a2 = new AccountDAO().getUser(a1);
        if (a1.getPassword().equals(a2.getPassword())) {
            try {
                session.setAttribute("account", a2);
                FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Alert", "Your Email or Password is incorrect"));
        }
    }

    public void logoutAccount() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            session.setAttribute("account", null);
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void processLogout() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            session.setAttribute("account", null);
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    List<Account> listAcc = new ArrayList<Account>();

    public List<Account> getListAcc() throws Exception {
        return listAcc = AccountDAO.displayAccount();
    }

    public void setListAcc(List<Account> listAcc) {
        this.listAcc = listAcc;
    }

    /**
     * Creates a new instance of AccountController
     */
    public AccountController() {
        hideLogin = "block";
        showBox = "none";
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public void deleteAccount() {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        new AccountDAO().deleteAccount(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Account has been deleted !"));
    }

    public void changeInformation() {
        try {
            ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
            HttpSession session = (HttpSession) e.getSession(true);
            Account a = (Account) session.getAttribute("account");
            if (a.getCompanyID() == 0) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("EditDriver.xhtml");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().redirect("EditCompany.xhtml");
            }
        } catch (IOException ex) {
            Logger.getLogger(AccountController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
